package com.qc.dao.impl;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.dao.WeiXinDao;
import com.qc.entity.WeiXinEntity;

@Repository("weiXinDao")
public class WeiXinDaoImpl implements WeiXinDao{

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	 
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public WeiXinEntity getWeiXinEntity() {
		
		 String sql = "select w_id,access_token,jsapi_ticket,appId,modified_time from t_weixin where 1 = 1 and if_del = '0' ";
		 WeiXinEntity weiXinEntity = new WeiXinEntity();
		 try {
			 weiXinEntity = (WeiXinEntity)jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(WeiXinEntity.class));
	        } catch (DataAccessException e) {
	        }
		 return weiXinEntity;
	}

	@Override
	public int saveWeiXin(WeiXinEntity weiXin) {
		
		String sql = "insert into t_weixin(access_token,jsapi_ticket,appId) values(?,?,?)";
		
        return jdbcTemplate.update(sql,new Object[]{weiXin.getAccessToken(),weiXin.getJsapiTicket(),weiXin.getAppId()});
	}

	@Override
	public int updateWeiXinId(WeiXinEntity weiXin) {
		
		String sql = "update t_weixin set access_token  = ?, jsapi_ticket = ?, appId  = ? where w_id = ? ";
		
        return jdbcTemplate.update(sql,new Object[]{weiXin.getAccessToken(),weiXin.getJsapiTicket(),weiXin.getAppId(),weiXin.getwId()});
	}

}
